package com.example.spring.mono.controller;

import com.example.spring.mono.entity.Order;
import com.example.spring.mono.service.OrderService;
import com.example.spring.mono.utils.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Valid;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/saveOrder")
    public Mono<Order> creatOrder(@Valid @RequestBody Order order) {
        // 新增和修改都是save，有id是修改，无id则是新增
        order.setOrderNo(null);
        // 检查用户名是否符合规则
        CheckUtil.checkName(order.getGoodsName());

        return orderService.saveOrder(order);
    }

    /**
     * 以数组形式一次返回全部数据
     */
    @GetMapping(value = "/get_all")
    public Flux<Order> getAll() {
        return orderService.getAll();
    }

    /**
     * 通过订单号查询订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("/find/{orderNo}")
    public Mono<ResponseEntity<Order>> findUserById(@PathVariable("orderNo") String orderNo){
        return orderService.findOrderByOrderNo(orderNo);
    }

    @DeleteMapping("/del/{oderNo}")
    public Mono<ResponseEntity<Void>> deleteOrder(@PathVariable("oderNo") String oderNo){
        return orderService.delOrderByOrderNo(oderNo);
    }

}
